#pragma once

#include <iostream>
#include <functional>
#include <ctime>
#include <unordered_map>
#include <sys/types.h>
#include <unistd.h>

using task_t = std::function<void()>;

void DownLoad()
{
    std::cout << "下载中……pid:" << getpid() << std::endl;
}
void Print()
{
    std::cout << "打印输出中……"<< getpid() << std::endl;
}
void UpDate()
{
    std::cout << "数据更新中……" << getpid()<< std::endl;
}

static int number_task = 0;

class TaskManerge
{
public:
    TaskManerge()
    {
        srand(time(nullptr));
        // 构造函数中初始化任务
        InsertTask(DownLoad);
        InsertTask(Print);
        InsertTask(UpDate);
    }
    void InsertTask(task_t t)
    {
        // 加入到map中
        tasks[number_task++] = t;
    }

    int SelectTask()
    {
        // 返回任务码
        return rand() % tasks.size();
    }

    void ExcuteTask(int number)
    {
        // 看任务是否在map中
        if (tasks.find(number) == tasks.end())
        {
            return;
        }
        // 执行
        tasks[number]();
    }

    ~TaskManerge()
    {
    }

private:
    std::unordered_map<int, task_t> tasks;
};

TaskManerge tm;